{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv(R\"G:\\Users\\yangjh\\Desktop\\repos\\statistic-2022\\data\\movie-country.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Unnamed: 0        int64\n",
       "id              float64\n",
       "average         float64\n",
       "country          object\n",
       "genre            object\n",
       "language         object\n",
       "release_date     object\n",
       "title            object\n",
       "votes           float64\n",
       "产地               object\n",
       "dtype: object"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = df.dropna()\n",
    "# df1.astype({\"votes\":\"int\",\"title\":\"string\"}).dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设定变量类型\n",
    "df2 = df1.astype({\"votes\":\"int\",\"title\":\"string\",\"产地\":\"category\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看类别变量所有的取值\n",
    "result = df2['产地'].cat.categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 按照不同水平值生成有序变量\n",
    "df2['星级'] = pd.cut(df2['average'],\n",
    "           bins=[0, 2, 4, 6, 8, 10],\n",
    "           labels=['一星', '二星', '三星', '四星', '五星'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Unnamed: 0         int64\n",
       "id               float64\n",
       "average          float64\n",
       "country           object\n",
       "genre             object\n",
       "language          object\n",
       "release_date      object\n",
       "title             string\n",
       "votes              int32\n",
       "产地              category\n",
       "星级              category\n",
       "dtype: object"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     五星\n",
       "1     五星\n",
       "2     五星\n",
       "3     五星\n",
       "4     五星\n",
       "5     五星\n",
       "6     五星\n",
       "7     五星\n",
       "8     五星\n",
       "9     五星\n",
       "10    五星\n",
       "11    五星\n",
       "12    五星\n",
       "13    五星\n",
       "14    五星\n",
       "15    五星\n",
       "16    五星\n",
       "17    五星\n",
       "18    四星\n",
       "19    五星\n",
       "20    五星\n",
       "21    四星\n",
       "22    五星\n",
       "23    五星\n",
       "24    五星\n",
       "25    五星\n",
       "26    五星\n",
       "27    五星\n",
       "28    五星\n",
       "29    五星\n",
       "30    五星\n",
       "31    五星\n",
       "32    五星\n",
       "33    五星\n",
       "34    五星\n",
       "35    五星\n",
       "36    五星\n",
       "37    五星\n",
       "38    五星\n",
       "39    四星\n",
       "40    五星\n",
       "41    五星\n",
       "42    五星\n",
       "43    五星\n",
       "44    四星\n",
       "45    五星\n",
       "46    五星\n",
       "48    五星\n",
       "49    四星\n",
       "50    四星\n",
       "51    五星\n",
       "52    五星\n",
       "53    四星\n",
       "54    五星\n",
       "55    四星\n",
       "56    四星\n",
       "57    五星\n",
       "58    五星\n",
       "Name: 星级, dtype: category\n",
       "Categories (5, object): ['一星' < '二星' < '三星' < '四星' < '五星']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2['星级'].cat.as_ordered()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     58\n",
       "unique    10\n",
       "top       美国\n",
       "freq      23\n",
       "Name: 产地, dtype: object"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2['产地'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_sav = pd.read_spss(R\"G:\\Users\\yangjh\\Desktop\\repos\\statistic-2022\\data\\identity.sav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "condition = '会以中国人自豪吗==\"会\" and 会隐瞒身份吗==\"一定会\"'\n",
    "df_sav_clean = df_sav.drop(df_sav.query(condition).index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "condition = '会以中国人自豪吗==\"会\" and 会隐瞒身份吗==\"一定会\"'\n",
    "df3 = df_sav.query(condition)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([ 69, 183, 184, 294, 347, 374, 394, 412, 448, 467, 498, 561, 566,\n",
       "            595, 612, 615, 625, 643, 742, 745, 757, 769, 777, 813, 819, 824,\n",
       "            829, 836, 843, 848],\n",
       "           dtype='int64')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3[['会以中国人自豪吗','会隐瞒身份吗']].index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>问卷编号</th>\n",
       "      <th>调查员</th>\n",
       "      <th>民族</th>\n",
       "      <th>政治面貌</th>\n",
       "      <th>年级</th>\n",
       "      <th>典型中国人</th>\n",
       "      <th>中国人特点</th>\n",
       "      <th>v1</th>\n",
       "      <th>v2</th>\n",
       "      <th>v3</th>\n",
       "      <th>...</th>\n",
       "      <th>会隐瞒身份吗</th>\n",
       "      <th>会打多少分</th>\n",
       "      <th>国歌升起</th>\n",
       "      <th>世博会</th>\n",
       "      <th>中国传统文化</th>\n",
       "      <th>发展信心</th>\n",
       "      <th>你会为中国运动员呐喊助威</th>\n",
       "      <th>遇到灾难时中国人应该伸出援手</th>\n",
       "      <th>你愿意加入其他国籍吗</th>\n",
       "      <th>中国人要为祖国统一奋斗吗</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>0 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [问卷编号, 调查员, 民族, 政治面貌, 年级, 典型中国人, 中国人特点, v1, v2, v3, v4, 你是否了解重活民族的传统节日, v5, 您觉得中国怎么样, 您认为中国有多少值得自豪的地方, 您认为世界有多少比例的人尊重中国, 对您而言作为一名中国人有多重要, 会以中国人自豪吗, 会隐瞒身份吗, 会打多少分, 国歌升起, 世博会, 中国传统文化, 发展信心, 你会为中国运动员呐喊助威, 遇到灾难时中国人应该伸出援手, 你愿意加入其他国籍吗, 中国人要为祖国统一奋斗吗]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 28 columns]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.drop(df3[['会以中国人自豪吗','会隐瞒身份吗']].index)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9650cb4e16cdd4a8e8e2d128bf38d875813998db22a3c986335f89e0cb4d7bb2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
